//
//  LLRouter.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLRouter.h"
#import "LLViewController.h"
#import "LLViewModel.h"

@interface LLRouter ()
@property(nonatomic, copy) NSDictionary *viewModelViewMappings; // viewModel到view的映射
@end

@implementation LLRouter

+ (instancetype)sharedInstance {
    static LLRouter *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (LLViewController *)viewControllerForViewModel:(LLViewModel *)viewModel {

    NSString *viewModelClassName = NSStringFromClass(viewModel.class);
    NSParameterAssert([viewModelClassName hasSuffix:@"ViewModel"]);
    NSString *viewControllerClassName = [NSString stringWithFormat:@"%@ViewController",
                                                                   [viewModelClassName substringWithRange:NSMakeRange(0, viewModelClassName.length - @"ViewModel".length)]];
    Class vc = NSClassFromString(viewControllerClassName);
    if ([vc isSubclassOfClass:[LLViewController class]] &&
            [vc instancesRespondToSelector:@selector(initWithViewModel:)]) {
        return [(LLViewController *) [vc alloc] initWithViewModel:viewModel];
    }

    NSString *viewController = self.viewModelViewMappings[NSStringFromClass(viewModel.class)];
    NSParameterAssert([NSClassFromString(viewController) isSubclassOfClass:[LLViewController class]]);
    NSParameterAssert([NSClassFromString(viewController) instancesRespondToSelector:@selector(initWithViewModel:)]);

    return [(LLViewController *) [NSClassFromString(viewController) alloc] initWithViewModel:viewModel];
}

- (NSDictionary *)viewModelViewMappings {
    return @{};
}

@end
